package dico.finder.impl;

import dico.finder.Word;
import dico.finder.WordFactory;
import dico.finder.WordType;

/**
 * a factory for decrypte the line in the forme 
 * 
 * mouftâtes,moufter|AEFMOSTTU,*V135
 * mouftée|EEFMOTU,*J21
 * 
 * @author thomas
 *
 */
public class DicoToWordFactory implements WordFactory {

	/**
	 * get the type of a word by the string . By example *V111 *N *O
	 * 
	 * @param type the string type
	 * 
	 * @return the object type
	 */
	public static WordType getType(final String type)
	{
		switch (type.charAt(1)) {
			case 'V' :
				return WordType.VERB;
			case 'N' :
				return WordType.COMMONNAME;
			case 'O' :
				return WordType.ONOMATOPEIA;
			case 'J' : 
				return WordType.ADJECTIVE;
			case 'W' : 
				return WordType.PROPERNAME;
			case 'I' : 
				return WordType.INTERJECTION;
			case 'Z' : 
				return WordType.ADVERB;
			case 'P' : 
				return WordType.PRONOUN;
			case 'D' : 
				return WordType.DETERMINANT;
			case 'R' : 
				return WordType.PREPOSITION;
			case 'C' : 
				return WordType.CONJUCTION;
			case 'A' : 
				return WordType.ABBREVIATION;
				
		}
		return WordType.UNKNOW;
	}
	
	@Override
	public Word create(final String line) {
		Word object_word = null;
		final int pos = line.indexOf("|");
		/** the word 1 word or 2 if it's a verb. be,be **/
		String word = line.substring(0,pos);
		/** informations about the word the alphabetics order and the type EB,*V111 */ 
		final String infos_word = line.substring(pos+1);
		
		/** get infotmation about the word */
		final int pos_2 = infos_word.indexOf(",");
		final String alphabetics_order = infos_word.substring(0,pos_2);
		final WordType type = DicoToWordFactory.getType(infos_word.substring(pos_2+1));
		
		
		/** get the word */
		if (type == WordType.VERB) {
			final int pos_3 = word.indexOf(",");
			final String infinitive = word.substring(pos_3+1);
			word = word.substring(0,pos_3);
			object_word = new VerbWord(word, infinitive, alphabetics_order);
		} else {
			object_word = new SimpleWord(word, type,alphabetics_order);
		}
		
		return object_word;
	}
	
	

}
